<template>
  <BasicModal @register="register" title="分配权限" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'WorkCalendarModal',
  });
</script>
<script setup lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { Icon } from '/@/components/Icon';
  import { updateAuth } from '/@/api/bas/cus/basUserCusScope';
  const emit = defineEmits(['success', 'register']);
  const { showMessage } = useMessage();
  let record = ref<any>();
  const [register, { closeModal, setModalProps }] = useModalInner(async (data) => {
    record.value = data;

    await resetFields();

    let inputFormSchemas: FormSchema[] = [
      {
        label: '权限类型',
        field: 'ctrlType',
        component: 'Select',
        componentProps: {
          dictType: 'ctrl_type',
        },
        required: true,
      },
    ];

    setProps({
      labelWidth: 120,
      schemas: inputFormSchemas,
      baseColProps: { lg: 24, md: 24 },
    });
  });

  // 保存数据
  async function handleSubmit() {
    try {
      const data = await validate();
      setModalProps({ confirmLoading: true });
      const params: any = {
        ids: record.value.ids,
        ...data,
      };
      const res = await updateAuth(params);
      showMessage(res.message);
      setTimeout(closeModal);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage('您填写的信息有误，请根据提示修正。');
      }
      console.log('error', error);
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
  const [registerForm, { resetFields, validate, setProps }] = useForm();
</script>
<style scoped></style>
